﻿namespace UnityEditor
{
    using System;
    using UnityEditorInternal;
    using UnityEngine;

    internal class AnimationClipInfoProperties
    {
        private SerializedProperty m_Property;

        public AnimationClipInfoProperties(SerializedProperty prop)
        {
            this.m_Property = prop;
        }

        public void AddCurve()
        {
            SerializedProperty property = this.Get("curves");
            if ((property != null) && property.isArray)
            {
                property.InsertArrayElementAtIndex(property.arraySize);
                property.GetArrayElementAtIndex(property.arraySize - 1).FindPropertyRelative("name").stringValue = "Curve";
                Keyframe[] keys = new Keyframe[] { new Keyframe(0f, 0f), new Keyframe(1f, 0f) };
                property.GetArrayElementAtIndex(property.arraySize - 1).FindPropertyRelative("curve").animationCurveValue = new AnimationCurve(keys);
            }
        }

        public void AssignToPreviewClip(AnimationClip clip)
        {
            MuscleClipInfo srcClipInfo = new MuscleClipInfo {
                startTime = this.firstFrame / clip.frameRate,
                stopTime = this.lastFrame / clip.frameRate,
                orientationOffsetY = this.orientationOffsetY,
                level = this.level,
                cycleOffset = this.cycleOffset,
                loopBlend = this.loopBlend,
                loopBlendOrientation = this.loopBlendOrientation,
                loopBlendPositionY = this.loopBlendPositionY,
                loopBlendPositionXZ = this.loopBlendPositionXZ,
                keepOriginalOrientation = this.keepOriginalOrientation,
                keepOriginalPositionY = this.keepOriginalPositionY,
                keepOriginalPositionXZ = this.keepOriginalPositionXZ,
                heightFromFeet = this.heightFromFeet,
                mirror = this.mirror,
                keepAdditionalBonesAnimation = this.keepAdditionalBonesAnimation
            };
            MuscleClipEditorUtilities.SetMuscleClipInfoNoDirty(clip, srcClipInfo);
        }

        public void BodyMaskFromClip(AvatarBodyMask mask)
        {
            SerializedProperty property = this.Get("bodyMask");
            if ((property != null) && property.isArray)
            {
                for (int i = 0; i < mask.GetBodyPartCount(); i++)
                {
                    mask.SetBodyPart(i, property.GetArrayElementAtIndex(i).intValue != 0);
                }
            }
        }

        public void BodyMaskToClip(AvatarBodyMask mask)
        {
            SerializedProperty property = this.Get("bodyMask");
            if ((property != null) && property.isArray)
            {
                for (int i = 0; i < mask.GetBodyPartCount(); i++)
                {
                    if (i >= property.arraySize)
                    {
                        property.InsertArrayElementAtIndex(i);
                    }
                    property.GetArrayElementAtIndex(i).intValue = !mask.GetBodyPart(i) ? 0 : 1;
                }
            }
        }

        public void ExtractFromPreviewClip(AnimationClip clip)
        {
            MuscleClipInfo muscleClipInfo = MuscleClipEditorUtilities.GetMuscleClipInfo(clip);
            if ((this.firstFrame / clip.frameRate) != muscleClipInfo.startTime)
            {
                this.firstFrame = this.FixPrecisionErrors(muscleClipInfo.startTime * clip.frameRate);
            }
            if ((this.lastFrame / clip.frameRate) != muscleClipInfo.stopTime)
            {
                this.lastFrame = this.FixPrecisionErrors(muscleClipInfo.stopTime * clip.frameRate);
            }
            this.orientationOffsetY = muscleClipInfo.orientationOffsetY;
            this.level = muscleClipInfo.level;
            this.cycleOffset = muscleClipInfo.cycleOffset;
            this.loopBlend = muscleClipInfo.loopBlend;
            this.loopBlendOrientation = muscleClipInfo.loopBlendOrientation;
            this.loopBlendPositionY = muscleClipInfo.loopBlendPositionY;
            this.loopBlendPositionXZ = muscleClipInfo.loopBlendPositionXZ;
            this.keepOriginalOrientation = muscleClipInfo.keepOriginalOrientation;
            this.keepOriginalPositionY = muscleClipInfo.keepOriginalPositionY;
            this.keepOriginalPositionXZ = muscleClipInfo.keepOriginalPositionXZ;
            this.heightFromFeet = muscleClipInfo.heightFromFeet;
            this.mirror = muscleClipInfo.mirror;
            this.keepAdditionalBonesAnimation = muscleClipInfo.keepAdditionalBonesAnimation;
        }

        private float FixPrecisionErrors(float f)
        {
            float num = Mathf.Round(f);
            if (Mathf.Abs((float) (f - num)) < 0.0001f)
            {
                return num;
            }
            return f;
        }

        private SerializedProperty Get(string property)
        {
            return this.m_Property.FindPropertyRelative(property);
        }

        public AnimationCurve GetCurve(int index)
        {
            AnimationCurve animationCurveValue = null;
            SerializedProperty property = this.Get("curves");
            if ((property != null) && property.isArray)
            {
                animationCurveValue = property.GetArrayElementAtIndex(index).FindPropertyRelative("curve").animationCurveValue;
            }
            return animationCurveValue;
        }

        public int GetCurveCount()
        {
            int arraySize = 0;
            SerializedProperty property = this.Get("curves");
            if ((property != null) && property.isArray)
            {
                arraySize = property.arraySize;
            }
            return arraySize;
        }

        public string GetCurveName(int index)
        {
            string stringValue = string.Empty;
            SerializedProperty property = this.Get("curves");
            if ((property != null) && property.isArray)
            {
                stringValue = property.GetArrayElementAtIndex(index).FindPropertyRelative("name").stringValue;
            }
            return stringValue;
        }

        public void RemoveCurve(int index)
        {
            SerializedProperty property = this.Get("curves");
            if ((property != null) && property.isArray)
            {
                property.DeleteArrayElementAtIndex(index);
            }
        }

        public void SetCurve(int index, AnimationCurve curve)
        {
            SerializedProperty property = this.Get("curves");
            if ((property != null) && property.isArray)
            {
                property.GetArrayElementAtIndex(index).FindPropertyRelative("curve").animationCurveValue = curve;
            }
        }

        public void SetCurveName(int index, string name)
        {
            SerializedProperty property = this.Get("curves");
            if ((property != null) && property.isArray)
            {
                property.GetArrayElementAtIndex(index).FindPropertyRelative("name").stringValue = name;
            }
        }

        public void SkeletonMaskFromClip(ref AvatarSkeletonMask mask)
        {
            SerializedProperty property = this.Get("skeletonMaskElements");
            if ((property != null) && property.isArray)
            {
                if ((property.arraySize > 0) && (mask.count != property.arraySize))
                {
                    mask.count = property.arraySize;
                }
                int count = mask.count;
                for (int i = 0; i < count; i++)
                {
                    SerializedProperty property2 = property.GetArrayElementAtIndex(i).FindPropertyRelative("path");
                    SerializedProperty property3 = property.GetArrayElementAtIndex(i).FindPropertyRelative("weight");
                    mask.SetPath(i, property2.stringValue);
                    mask.SetWeight(i, property3.floatValue);
                }
            }
        }

        public void SkeletonMaskToClip(AvatarSkeletonMask mask)
        {
            SerializedProperty serializedProperty = this.Get("skeletonMaskElements");
            ModelImporter.UpdateSkeletonMask(mask, serializedProperty);
        }

        public float cycleOffset
        {
            get
            {
                return this.Get("cycleOffset").floatValue;
            }
            set
            {
                this.Get("cycleOffset").floatValue = value;
            }
        }

        public float firstFrame
        {
            get
            {
                return this.Get("firstFrame").floatValue;
            }
            set
            {
                this.Get("firstFrame").floatValue = value;
            }
        }

        public bool heightFromFeet
        {
            get
            {
                return this.Get("heightFromFeet").boolValue;
            }
            set
            {
                this.Get("heightFromFeet").boolValue = value;
            }
        }

        public bool keepAdditionalBonesAnimation
        {
            get
            {
                return this.Get("keepAdditionalBonesAnimation").boolValue;
            }
            set
            {
                this.Get("keepAdditionalBonesAnimation").boolValue = value;
            }
        }

        public bool keepOriginalOrientation
        {
            get
            {
                return this.Get("keepOriginalOrientation").boolValue;
            }
            set
            {
                this.Get("keepOriginalOrientation").boolValue = value;
            }
        }

        public bool keepOriginalPositionXZ
        {
            get
            {
                return this.Get("keepOriginalPositionXZ").boolValue;
            }
            set
            {
                this.Get("keepOriginalPositionXZ").boolValue = value;
            }
        }

        public bool keepOriginalPositionY
        {
            get
            {
                return this.Get("keepOriginalPositionY").boolValue;
            }
            set
            {
                this.Get("keepOriginalPositionY").boolValue = value;
            }
        }

        public float lastFrame
        {
            get
            {
                return this.Get("lastFrame").floatValue;
            }
            set
            {
                this.Get("lastFrame").floatValue = value;
            }
        }

        public float level
        {
            get
            {
                return this.Get("level").floatValue;
            }
            set
            {
                this.Get("level").floatValue = value;
            }
        }

        public bool loop
        {
            get
            {
                return this.Get("loop").boolValue;
            }
            set
            {
                this.Get("loop").boolValue = value;
            }
        }

        public bool loopBlend
        {
            get
            {
                return this.Get("loopBlend").boolValue;
            }
            set
            {
                this.Get("loopBlend").boolValue = value;
            }
        }

        public bool loopBlendOrientation
        {
            get
            {
                return this.Get("loopBlendOrientation").boolValue;
            }
            set
            {
                this.Get("loopBlendOrientation").boolValue = value;
            }
        }

        public bool loopBlendPositionXZ
        {
            get
            {
                return this.Get("loopBlendPositionXZ").boolValue;
            }
            set
            {
                this.Get("loopBlendPositionXZ").boolValue = value;
            }
        }

        public bool loopBlendPositionY
        {
            get
            {
                return this.Get("loopBlendPositionY").boolValue;
            }
            set
            {
                this.Get("loopBlendPositionY").boolValue = value;
            }
        }

        public bool mirror
        {
            get
            {
                return this.Get("mirror").boolValue;
            }
            set
            {
                this.Get("mirror").boolValue = value;
            }
        }

        public string name
        {
            get
            {
                return this.Get("name").stringValue;
            }
            set
            {
                this.Get("name").stringValue = value;
            }
        }

        public float orientationOffsetY
        {
            get
            {
                return this.Get("orientationOffsetY").floatValue;
            }
            set
            {
                this.Get("orientationOffsetY").floatValue = value;
            }
        }

        public string takeName
        {
            get
            {
                return this.Get("takeName").stringValue;
            }
            set
            {
                this.Get("takeName").stringValue = value;
            }
        }

        public int wrapMode
        {
            get
            {
                return this.Get("wrapMode").intValue;
            }
            set
            {
                this.Get("wrapMode").intValue = value;
            }
        }
    }
}

